package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service

public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper baseMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    RoomLabelMapper roomLabelMapper;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    RoomFacilityMapper roomFacilityMapper;



    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {





        return baseMapper.selectPageItem(roomItemVoPage, queryVo);

    }



    @Override
    public void removeByIdAll(Long id) {
        //删除房间信息
        RoomInfoMapper.removeById(id);

        //删除房间标签
        LambdaQueryWrapper<LabelInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabelInfo::getId,id);
        lambdaQueryWrapper.eq(LabelInfo::getType,2);
        labelInfoMapper.delete(lambdaQueryWrapper);

        //删除房间图片\
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        //删除房间配套
        LambdaQueryWrapper<FacilityInfo> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getId,id);
        facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getType,2);
        facilityInfoMapper.delete(facilityInfoLambdaQueryWrapper);

        //删除房间属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getId,id);
        roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);



        //删除房间租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getId,id);
        roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);


        //删除房间支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getId,id);
        roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);




    }

    @Override
    public void updateRoom(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();
        if (id != null) {
            //删除房间标签
            LambdaQueryWrapper<LabelInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabelInfo::getId,id);
            lambdaQueryWrapper.eq(LabelInfo::getType,2);
            labelInfoMapper.delete(lambdaQueryWrapper);

            //删除房间图片\
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getId,id);
            graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

            //删除房间配套
            LambdaQueryWrapper<FacilityInfo> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getId,id);
            facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getType,2);
            facilityInfoMapper.delete(facilityInfoLambdaQueryWrapper);

            //删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getId,id);
            roomAttrValueMapper.delete(roomAttrValueLambdaQueryWrapper);



            //删除房间租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getId,id);
            roomLeaseTermMapper.delete(roomLeaseTermLambdaQueryWrapper);


            //删除房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getId,id);
            roomPaymentTypeMapper.delete(roomPaymentTypeLambdaQueryWrapper);
        }else{

        }

    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<RoomInfo> roomInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        roomInfoLambdaUpdateWrapper.eq(RoomInfo::getId,id)
                .in(RoomInfo::getIsRelease,ReleaseStatus.RELEASED,ReleaseStatus.NOT_RELEASED)
                .set(RoomInfo::getIsRelease,status);
        baseMapper.update(null,roomInfoLambdaUpdateWrapper);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {

        RoomInfo roomInfo = baseMapper.selectById(id);
        Long roomInfoId = roomInfo.getId();


        //获取图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setUrl(graphInfo.getUrl());
            graphVo.setName(graphInfo.getName());
            graphVoList.add(graphVo);
        }


        //获取配套
        List<FacilityInfo>facilityInfoList=facilityInfoMapper.selectRoomFaciltyList(id);

        //获取属性
        List<AttrValueVo>attrValueVoList=roomAttrValueMapper.selectRoomAttrValueList(id);

        //获取租期
        List<LeaseTerm>leaseTermVoList=roomLeaseTermMapper.selectRoomLeaseTermList(id);
        //获取支付方式
        List<PaymentType>paymentTypeList=roomPaymentTypeMapper.selectRoomPaymentTypeList(id);
        //获取标签
        List<LabelInfo>labelInfoList=labelInfoMapper.selectRoomLabelList(id);

        //获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById((roomInfo.getApartmentId()));

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);


        return roomDetailVo;
    }
}




